// 烘焙圈的接口
//引入express模块
const express = require("express");
//引入连接池模块
const pool = require("../pool.js");
//console.log(pool)
//创建路由器对象
const router = express.Router();
//获取用户信息的接口
//GET /circle
//地址:http://localhost:3333/circle
//返回结果
router.get("/", (req, res, next) => {
  
  console.log(req.query);
  //2:定义sql语句]
  let sql = " select * from  hbb_circle";
  //3:执行数据库操作
  pool.query(sql,(err, results) => {
    if (err) {
      next(err);
      return;
    }
    res.send({ code: 200, msg: "已获取内容表信息", data: results });
  });
});
// 通过eid查询用户信息
//GET /circle/list
//地址:http://localhost:3333/circle/list?eid=
router.get("/list", (req, res, next) => {
  
  console.log(req.query);
  //2:定义sql语句]
  let sql = " select * from  hbb_circle where eid=?";
  //3:执行数据库操作
  pool.query(sql,[req.query.eid],(err, results) => {
    if (err) {
      next(err);
      return;
    }
    res.send({ code: 200, msg: "已获取内容表信息", data: results });
  });
});


// // 增加内容
//GET /circle/add
//地址:http://localhost:3333/circle/add?....
router.post("/add", (req, res, next) => { 
  console.log(req.body);
  //2:定义sql语句]
  let sql = "insert into hbb_circle set ?";
  //3:执行数据库操作
  pool.query(sql,[req.body],(err, results) => {
    if (err) {
      next(err);
      return;
    }
    res.send({ code: 200, msg: "已插入内容表信息", data: results });
  });
});
// 修改内容
router.post("/update", (req, res, next) => { 
    console.log(req.body);
    //2:定义sql语句
    let sql = "update hbb_circle set ? where eid=?";
    //info=?,img=?,content=?,content=? 
    //3:执行数据库操作
    pool.query(sql,[req.body,req.body.eid],(err, results) => {
      if (err) {
        next(err);
        return;
      }
      res.send({ code: 200, msg: "已更新内容表信息", data: results });
    });
});
// // 删除内容
// router.get("/delete", (req, res, next) => { 
//     console.log(req.query);
//     //2:定义sql语句]
//     let sql = "delete *from hbb_circle where eid=?";
//     //3:执行数据库操作
//     pool.query(sql,[req.query.eid],(err, results) => {
//       if (err) {
//         next(err);
//         return;
//       }
//       res.send({ code: 200, msg: "已删除内容表信息", data: results });
//     });
// });
//暴露路由器对象
module.exports = router;